<template>
  <el-card>
    <h3>医疗服务报销申请</h3>
    <el-form :model="medicalServiceForm" label-width="150px" ref="medicalServiceForm">
      <!-- medicalserviceid (不可编辑) -->
      <el-form-item label="医疗服务ID">
        <el-input v-model="medicalServiceForm.medicalserviceid" disabled placeholder="自动生成"></el-input>
      </el-form-item>

      <!-- insuredpersonid (不可编辑) -->
      <el-form-item label="参保人ID">
        <el-input v-model="medicalServiceForm.insuredpersonid" disabled placeholder="已关联"></el-input>
      </el-form-item>

      <!-- servicedate (可编辑) -->
      <el-form-item label="服务发生日期">
        <el-date-picker v-model="medicalServiceForm.servicedate" type="date" placeholder="选择日期"></el-date-picker>
      </el-form-item>

      <!-- medicalinstitutionname (文本框输入) -->
      <el-form-item label="医保机构">
        <el-input v-model="medicalServiceForm.medicalinstitutionid" placeholder="请输入医保机构编号"></el-input>
      </el-form-item>

      <!-- diagnosis (可编辑) -->
      <el-form-item label="诊断结果">
        <el-input v-model="medicalServiceForm.diagnosis" placeholder="请输入诊断结果"></el-input>
      </el-form-item>

      <!-- treatment (可编辑) -->
      <el-form-item label="治疗方案">
        <el-input v-model="medicalServiceForm.treatment" placeholder="请输入治疗方案"></el-input>
      </el-form-item>

      <!-- totalcost (可编辑) -->
      <el-form-item label="医疗服务总费用">
        <el-input v-model="medicalServiceForm.totalcost" placeholder="请输入总费用" type="number"></el-input>
      </el-form-item>

      <!-- Confirm Button -->
      <el-form-item>
        <el-button type="primary" @click="submitForm">确认保存</el-button>
        <el-button @click="resetForm">重置表单</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      medicalServiceForm: {
        medicalserviceid: '自动生成',    // 自动生成的医疗服务ID，不可编辑// 参保人ID，不可编辑
        servicedate: '',
      insuredpersonid: '',// 服务发生日期
        medicalinstitutionid: '',     // 医保机构名称
        diagnosis: '',                  // 诊断结果
        treatment: '',                  // 治疗方案
        totalcost: ''                   // 医疗服务总费用
      }
    };
  },
  mounted() {
    // 从路由参数中获取参保人ID
    this.medicalServiceForm.insuredpersonid = this.$route.params.username;
  },
  methods: {
    submitForm() {
      // 验证并提交表单
      this.$refs.medicalServiceForm.validate((valid) => {
        if (valid) {
          // 克隆对象并移除 medicalserviceid 字段
          const formData = { ...this.medicalServiceForm };
          delete formData.medicalserviceid;
          formData.servicedate = formData.servicedate ? formData.servicedate.toISOString().split('T')[0] : '';
          // 提交数据到服务器

          console.log('提交的表单数据:', formData);
          axios.post('http://localhost:8081/medicalService/add', formData)
              .then(response => {
                console.log('Form submitted:', response.data);
                this.$message.success('医疗服务报销申请已提交！');
              })
              .catch(error => {
                console.error('Error submitting form:', error);
                this.$message.error('提交失败，请稍后再试。');
              });
        } else {
          this.$message.error('表单填写有误，请检查后提交。');
        }
      });
    },
    resetForm() {
      // 重置表单内容
      this.$refs.medicalServiceForm.resetFields();
    }
  }
};
</script>

<style scoped>
h3 {
  color: #333;
  margin-bottom: 20px;
}
</style>
